package com.fyy.controller;

import com.fyy.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author fyy
 * @Description
 * @Date 2020/6/1 10:42
 */
@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

//    private static final String rest_url = "http://localhost:8081";
    //ribbon实现，请求地址应该是一个变量，及我们注册eureka的服务名称
    private static final String rest_url = "http://PEOUSER";

    @RequestMapping("/con/user/{id}")
    public User getser(@PathVariable("id") long id){
         return restTemplate.getForObject(rest_url+"/query/"+id,User.class);
    }

    @RequestMapping("/con/user/save")
    public boolean saveUser(String username,String pass){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username",username);
        params.put("pass",pass);
        return restTemplate.postForObject(rest_url+"/add",params,Boolean.class);
    }

    @RequestMapping("/query")
    public List<User> queryAllUser(){
        return restTemplate.getForObject(rest_url+"/api/user/query",List.class);
    }



}
